<?php

// 引入thinkphp的http库和image库
import("ORG.Net.Http");
import("ORG.Util.Image");

class news {
	/* 
	 * 将url内容解析出来,拼成单图文素材
	 * 
	 * http://localhost/news/index.json/?time=2011-3-4 12:10:10
	 * */

	public static function getNewsMaterialFromURL($url=null,$cid=null){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		if($url){
			$lines_string=file_get_contents($url);
			//读取title--$head[1]
			eregi("<title>(.*)</title>", $lines_string, $title);
			
			//读取图片img
			// 形如:<img src="http://mmsns.qpic.cn/mmsns/3EEJ2RzlMBy0iakmYIRJTNib5Bf5jia3Lz6U4SmKThYsfDOmohajmrssw/0" onerror="this.parentNode.removeChild(this)"/>
			//<img src="http://res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/big_loading19d82d.gif" onerror="this.parentNode.removeChild(this)" data-src="http://mmsns.qpic.cn/mmsns/3EEJ2RzlMBxJuNXeRS6WE3EbP3utcCMhjVAjPxSoX42UBqdFuiazc1A/0" />
			//img 的链接有src改为是data-src 2013-11-28
			preg_match('/<img onerror=.* src[^>]*>/Ui',$lines_string, $imgSrc);
// 			preg_match('/<img[^>]*>/Ui',$lines_string, $imgSrc);
			preg_match_all('/src="(.*)"/iUs', $imgSrc[0], $imgUrl);
			//imgUrl-$imgUrl[1][0]
			
			//preg_match_all('/data-src="(.*)"/iUs', $imgSrc[0], $imgUrl);

			//读取正文文字--$content[1]摘要已经更换
// 			preg_match('/<script[^>]*id="txt-desc"[^>]*>(.*?)<\/script>/si',$lines_string,$description);
// 			$description[1];

			//摘要内容 在微信分享函数中
// 			$onBridgeReadyArray = explode('onBridgeReady()', $lines_string);
			//去掉空格
// 			foreach ($onBridgeReadyArray as $onbriItem){
// 				$onbriItem = str_replace(' ','',trim($onbriItem));
// 				if(stripos($onbriItem,'desc=htmlDecode') !== false){
// 					preg_match('/desc=htmlDecode\(.*\)/',trim($onbriItem),$des);
// 					preg_match_all('/desc=htmlDecode\("(.*)"\)/iUs',$des[0],$descriptionArr);
						
// 					$description = $descriptionArr[1][0];
// 				}
// 			}

			//摘要内容 在变量中
// 			eregi('msg_desc = \".*\"', $lines_string, $description);
			preg_match_all('/msg_desc = \".*\";/',$lines_string, $description);
			if ($description) {
				$description = substr($description[0][0], strlen("msg_desc = \""), strlen($description[0][0]) - strlen("msg_desc = \"") - 2);
			}
		//	
// 			print_r($description);
			
// 			$onBridgeReadyArray[1] = str_replace(' ','',trim($onBridgeReadyArray[1]));
// 			preg_match('/desc=htmlDecode\(.*\)/',trim($onBridgeReadyArray[1]),$des);
// 			preg_match_all('/desc=htmlDecode\("(.*)"\)/iUs',$des[0],$descriptionArr);
			
// 			$description = $descriptionArr[1][0];
			
			
			//正文
// 			preg_match('/<div[^>]*class="text"[^>]*>(.*?)<\/div>/si',$lines_string,$text);
			
// 			$array = explode('</p>', $text[1]);
// 			$description= $array[0];
// 			$description = str_replace(array("<p>",'<p style="">'), '', $description);
			
// 			//替换正文中img
// 			$array = explode('<img', $text[1]);
// 			foreach ($array as $textitem){
// // 				preg_match('/<img[^>]*>/Ui',$description[1], $textimgSrc);
// 				//imgUrl-$imgUrl[1][0]
// 				preg_match_all('/data-src="(.*)"/iUs', $textitem, $textimgUrl);
				
// 				$description[1] = str_replace($textimgUrl[1][0],
// 						get_cache_image($textimgUrl[1][0]),
// 						$text[1]);
// 			}
			
// 			$text[1] = str_replace('data-src','src',$text[1]);
			
// 			var_dump($title[1]);
// 			var_dump($description);
			//素材信息关键字段都存在时返回正确素材
			if($title[1]){
// 			if($title[1] &&  $description && $imgUrl[1][0]){
				$item = new stdClass();
				$item->title = $title[1];
				if ($description) {
					$item->description = trim($description);
				} else {
					$item->description = "";
				}
				if(isset($imgUrl[1][0])&& $imgUrl[1][0] !== '' && $imgUrl[1][0]!= null){
					$item->picUrl = get_cache_image($imgUrl[1][0]);
				}else{
					$item->picUrl = null;
				}
				$item->url = $url;
				
				$xmlString = news::xml_content($item);
				$result = new stdClass();
				$result->news = $xmlString;
				Output::instance()->module('news', 'getNewsMaterialFromURL')->object($result);
			}else{
				
				Output::instance()->resultCode(code_news_material_url_invalid);
			}
			
		}else{
			Output::instance()->resultCode(code_news_material_url_not_exist);
		}
	}
	
	private function xml_content($item) {
		$xml = array();
		
		news::xml_append($xml, '<xml>');
		news::xml_append($xml, '<Articles>');
		news::xml_append($xml, '<item>');
			
		news::xml_append($xml, '<title><![CDATA[' . $item->title . ']]></title>');
		news::xml_append($xml, '<description><![CDATA[' . $item->description . ']]></description>');
		news::xml_append($xml, '<picUrl><![CDATA[' . $item->picUrl . ']]></picUrl>');
		news::xml_append($xml, '<url><![CDATA[' . $item->url . ']]></url>');
			
		news::xml_append($xml, '</item>');
		
		news::xml_append($xml, '</Articles>');
		news::xml_append($xml, '</xml>');
		
		return implode("",$xml);
	}
	
	private function xml_append(&$xml, $str) {
		array_push($xml, $str);
	}
	
	
	
	
}